<template>
  <div :class="className" :style="{height, width}" />
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount, watch } from 'vue'
import * as echarts from 'echarts'
import { debounce } from '@/utils/index.js'

const props = defineProps({
  className: {
    type: String,
    default: 'chart-line'
  },
  width: {
    type: String,
    default: '100%'
  },
  height: {
    type: String,
    default: '350px'
  },
  autoResize: {
    type: Boolean,
    default: true
  },
  chartData: {
    type: Object,
    required: true
  }
})

const chart = ref(null)
let resizeHandler = null

const setOptions = ({successData = [], failData = [], runningData = [], lineCharDates = []} = {}) => {
  chart.value.setOption({
    xAxis: {
      data: lineCharDates,
      boundaryGap: false,
      axisTick: {
        show: false
      }
    },
    grid: {
      left: 10,
      right: 10,
      bottom: 20,
      top: 30,
      containLabel: true
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      },
      padding: [5, 10]
    },
    yAxis: {
      axisTick: {
        show: false
      }
    },
    legend: {
      data: ['成功', '失败', '进行中']
    },
    series: [{
      name: '成功',
      itemStyle: {
        normal: {
          color: '#9ACD32',
          lineStyle: {
            color: '#9ACD32',
            width: 2
          }
        }
      },
      smooth: true,
      type: 'line',
      data: successData,
      animationDuration: 2800,
      animationEasing: 'cubicInOut'
    },
    {
      name: '失败',
      smooth: true,
      type: 'line',
      itemStyle: {
        normal: {
          color: '#FF005A',
          lineStyle: {
            color: '#FF005A',
            width: 2
          }
        }
      },
      data: failData,
      animationDuration: 2800,
      animationEasing: 'quadraticOut'
    },
    {
      name: '进行中',
      smooth: true,
      type: 'line',
      itemStyle: {
        normal: {
          color: '#FFD700',
          lineStyle: {
            color: '#FFD700',
            width: 2
          }
        }
      },
      data: runningData,
      animationDuration: 2800,
      animationEasing: 'quadraticOut'
    }]
  })
}

const initChart = () => {
  chart.value = echarts.init(document.querySelector(`.${props.className}`))
  setOptions(props.chartData)
}

watch(() => props.chartData, (val) => {
  setOptions(val)
}, { deep: true })

onMounted(() => {
  initChart()
  if (props.autoResize) {
    resizeHandler = debounce(() => {
      if (chart.value) {
        chart.value.resize()
      }
    }, 100)
    window.addEventListener('resize', resizeHandler)
  }

  const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
  sidebarElm?.addEventListener('transitionend', resizeHandler)
})

onBeforeUnmount(() => {
  if (!chart.value) {
    return
  }
  
  if (props.autoResize) {
    window.removeEventListener('resize', resizeHandler)
  }

  const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
  sidebarElm?.removeEventListener('transitionend', resizeHandler)

  chart.value.dispose()
  chart.value = null
})
</script>
